package cn.com.git.admin.service.impl;

import cn.com.git.admin.dao.*;
import cn.com.git.admin.dao.model.*;
import cn.com.git.admin.service.SystemService;
import cn.com.git.admin.service.dto.*;
import cn.com.git.admin.service.query.*;
import cn.com.git.common.base.BaseServiceImpl;
import cn.com.git.common.model.DataTree;
import cn.com.git.common.model.ResultConstant;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import cn.com.git.common.util.object.MapperFacadeUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* System ServiceImpl 接口
* @author zhangzhenfu
* @Description
* @date 2020/5/8
*/
@Transactional
@Service
public class SystemServiceImpl extends BaseServiceImpl implements SystemService {

    @Autowired
    private SysResourceDAO sysResourceDAO;
    @Autowired
    private SysOperateLogDAO sysOperateLogDAO;
    @Autowired
    private SysNoteDictDAO sysNoteDictDAO;
    @Autowired
    private SysMessageDAO sysMessageDAO;
    @Autowired
    private SysKeyValueDAO sysKeyValueDAO;

    /*=================================*/
    /*============ SysResource =============*/
    /*=================================*/
    @Override
    public ResultPage<SysResourceListDTO> listSysResource(SysResourceListQuery query) {
        List<SysResourceListDTO> data = new ArrayList<>();
        List<SysResource> list = sysResourceDAO.selectList(query);
            Optional.ofNullable(list).orElse(Lists.newArrayList())
            .stream().forEach(item->{
                SysResourceListDTO d = new SysResourceListDTO();
                d.setM(MapperFacadeUtils.map(item,SysResourceListDTO.SysResource.class));
                data.add(d);
            });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<List<DataTree>> listTreeSysResource(SysResourceListQuery query) {
        List<DataTree> data = new ArrayList<>();
        List<SysResource> list = Optional.ofNullable(sysResourceDAO.selectList(query)).orElse(Lists.newArrayList()) ;
        if(list.size()>0){
            list.stream().forEach(s->{
                DataTree tree = new DataTree(s.getId(),s.getPermission(),s.getName(),s.getLevel(),s.getPriority(),s.getPid());
                data.add(tree);
            });
//            DataTree.transformTree(data);
        }
        return new ResultMsg<>(ResultConstant.SUCCESS,null,data);
    }

    @Override
    public ResultMsg<SysResourceDetailDTO> findSysResource(Integer id) {
        SysResourceDetailDTO d = new SysResourceDetailDTO();
        SysResource sysResource = sysResourceDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(sysResource,SysResourceDetailDTO.SysResource.class));
        if(sysResource.getPid()!=null){
            SysResource pSysResource = sysResourceDAO.selectById(sysResource.getPid());
            d.setParent(MapperFacadeUtils.map(pSysResource,SysResourceDetailDTO.SysResource.class));
        }
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<SysResourceSaveDTO> addSysResource(SysResourceSaveDTO saveDTO) {
        Integer id = sysResourceDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),SysResource.class));
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<SysResourceSaveDTO> editSysResource(SysResourceSaveDTO saveDTO) {
        sysResourceDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),SysResource.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteSysResource(Integer id) {
        sysResourceDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }


    /*=================================*/
    /*============ SysOperateLog =============*/
    /*=================================*/
    @Override
    public ResultPage<SysOperateLogListDTO> listSysOperateLog(SysOperateLogListQuery query) {
        List<SysOperateLogListDTO> data = new ArrayList<>();
        List<SysOperateLog> list = sysOperateLogDAO.selectList(query);
        Optional.ofNullable(list).orElse(Lists.newArrayList())
                .stream().forEach(item->{
            SysOperateLogListDTO d = new SysOperateLogListDTO();
            d.setM(MapperFacadeUtils.map(item,SysOperateLogListDTO.SysOperateLog.class));
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<SysOperateLogDetailDTO> findSysOperateLog(Integer id) {
        SysOperateLogDetailDTO d = new SysOperateLogDetailDTO();
        SysOperateLog sysOperateLog = sysOperateLogDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(sysOperateLog,SysOperateLogDetailDTO.SysOperateLog.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<SysOperateLogSaveDTO> addSysOperateLog(SysOperateLogSaveDTO saveDTO) {
        Integer id = sysOperateLogDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),SysOperateLog.class));
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<SysOperateLogSaveDTO> editSysOperateLog(SysOperateLogSaveDTO saveDTO) {
        sysOperateLogDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),SysOperateLog.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteSysOperateLog(Integer id) {
        sysOperateLogDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }

    /*=================================*/
    /*============ SysNoteDict =============*/
    /*=================================*/
    @Override
    public ResultPage<SysNoteDictListDTO> listSysNoteDict(SysNoteDictListQuery query) {
        List<SysNoteDictListDTO> data = new ArrayList<>();
        List<SysNoteDict> list = sysNoteDictDAO.selectList(query);
        Optional.ofNullable(list).orElse(Lists.newArrayList())
                .stream().forEach(item->{
            SysNoteDictListDTO d = new SysNoteDictListDTO();
            d.setM(MapperFacadeUtils.map(item,SysNoteDictListDTO.SysNoteDict.class));
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<SysNoteDictDetailDTO> findSysNoteDict(Integer id) {
        SysNoteDictDetailDTO d = new SysNoteDictDetailDTO();
        SysNoteDict sysNoteDict = sysNoteDictDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(sysNoteDict,SysNoteDictDetailDTO.SysNoteDict.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<SysNoteDictSaveDTO> addSysNoteDict(SysNoteDictSaveDTO saveDTO) {
        Integer id = sysNoteDictDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),SysNoteDict.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<SysNoteDictSaveDTO> editSysNoteDict(SysNoteDictSaveDTO saveDTO) {
        sysNoteDictDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),SysNoteDict.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteSysNoteDict(Integer id) {
        sysNoteDictDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }

    /*=================================*/
    /*============ SysMessage =============*/
    /*=================================*/
    @Override
    public ResultPage<SysMessageListDTO> listSysMessage(SysMessageListQuery query) {
        List<SysMessageListDTO> data = new ArrayList<>();
        List<SysMessage> list = sysMessageDAO.selectList(query);
        Optional.ofNullable(list).orElse(Lists.newArrayList())
                .stream().forEach(item->{
            SysMessageListDTO d = new SysMessageListDTO();
            d.setM(MapperFacadeUtils.map(item,SysMessageListDTO.SysMessage.class));
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<SysMessageDetailDTO> findSysMessage(Integer id) {
        SysMessageDetailDTO d = new SysMessageDetailDTO();
        SysMessage sysMessage = sysMessageDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(sysMessage,SysMessageDetailDTO.SysMessage.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<SysMessageSaveDTO> addSysMessage(SysMessageSaveDTO saveDTO) {
        Integer id = sysMessageDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),SysMessage.class));
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<SysMessageSaveDTO> editSysMessage(SysMessageSaveDTO saveDTO) {
        sysMessageDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),SysMessage.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteSysMessage(Integer id) {
        sysMessageDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }


    /*=================================*/
    /*============ SysKeyValue =============*/
    /*=================================*/
    @Override
    public ResultPage<SysKeyValueListDTO> listSysKeyValue(SysKeyValueListQuery query) {
        List<SysKeyValueListDTO> data = new ArrayList<>();
        List<SysKeyValue> list = sysKeyValueDAO.selectList(query);
        Optional.ofNullable(list).orElse(Lists.newArrayList())
                .stream().forEach(item->{
            SysKeyValueListDTO d = new SysKeyValueListDTO();
            d.setM(MapperFacadeUtils.map(item,SysKeyValueListDTO.SysKeyValue.class));
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<SysKeyValueDetailDTO> findSysKeyValue(Integer id) {
        SysKeyValueDetailDTO d = new SysKeyValueDetailDTO();
        SysKeyValue sysKeyValue = sysKeyValueDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(sysKeyValue,SysKeyValueDetailDTO.SysKeyValue.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<SysKeyValueSaveDTO> addSysKeyValue(SysKeyValueSaveDTO saveDTO) {
        Integer id = sysKeyValueDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),SysKeyValue.class));
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<SysKeyValueSaveDTO> editSysKeyValue(SysKeyValueSaveDTO saveDTO) {
        sysKeyValueDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),SysKeyValue.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteSysKeyValue(Integer id) {
        sysKeyValueDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }
}